package com.tourismhengyang.actions;

import javax.servlet.http.HttpSession;

import org.apache.struts2.ServletActionContext;

import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ModelDriven;
import com.tourismhengyang.beans.Admin;
import com.tourismhengyang.biz.IAdminBiz;
import com.tourismhengyang.impl.AdminBizImpl;
import com.tourismhengyang.utils.JsonObject;

public class LoginAction extends ActionSupport implements ModelDriven<Admin>{
	private static final long serialVersionUID = 1L;
	private Admin admin;
	private JsonObject json=new JsonObject();
	private HttpSession session;
	private IAdminBiz adminBiz=new AdminBizImpl();
	{
		session=ServletActionContext.getRequest().getSession();
	}
	
	public String login(){
		Admin currentLogin=adminBiz.checkLoginInfo(admin);
		if(currentLogin!=null){
			json.setObj(1);
			session.setAttribute("adminInfo", currentLogin);
		}else{
			json.setObj(0);
		}
		return SUCCESS;
	}
	
	public String updatePwd(){
		Admin ad=(Admin) session.getAttribute("adminInfo");
		ad.setAdminPwd(admin.getAdminPwd());
		System.out.println(ad);
		int result=adminBiz.updatePwd(ad);
		json=new JsonObject();
		json.setObj(result);
		session.removeAttribute("adminInfo");
		return SUCCESS;
	}
	
	public String exitSystem(){
		session.removeAttribute("adminInfo");
		if(session.getAttribute("adminInfo")==null){
			json=new JsonObject();
			json.setObj(1);
			return SUCCESS;
		}
		return null;
	}
	
	public JsonObject getJson() {
		return json;
	}

	@Override
	public Admin getModel() {
		if(admin==null){
			admin=new Admin();
		}
		return admin;
	}
}
